package org.soarsky.wealthchengdu.module.home;

import org.soarsky.wealthchengdu.module.info.FocusCDDataShow;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 所有画廊的显示
 * 
 * 
 */
public class AllGalleryShow {
	private static AllGalleryShow allgallery;

	/**
	 * 获取实例
	 * 
	 * @return
	 */
	
	public static AllGalleryShow getinstanise() {
		if (allgallery == null) {
			allgallery = new AllGalleryShow();
		}
		return allgallery;
	}

	/**
	 * 设置小圆点
	 * 
	 * @param context
	 * @param size
	 * @param imgnumberliner
	 */
	public void setLinerNumber(Gallery imgviewGaller, Context context,
			int size, LinearLayout imgnumberliner, int down, int up,
			ImageView[] imageviews) {
		for (int i = 0; i < size; i++) {
			ImageView img = new ImageView(context);
			imageviews[i] = img;
			if (i == (int) size / 2) {
				imageviews[i].setImageResource(down);
			} else {
				imageviews[i].setImageResource(up);
			}
			imageviews[i].setPadding(2, 0, 2, 0);
			imgnumberliner.addView(imageviews[i]);

		}
		Galleryclilck(context,imgviewGaller, imgnumberliner, down, up, imageviews);
	}

	/**
	 * 设置画廊滑动事件
	 * 
	 * @param imgviewGaller
	 * @param imgnumberliner
	 */
	private void Galleryclilck(final Context context,Gallery imgviewGaller,
			final LinearLayout imgnumberliner, final int down, final int up,
			final ImageView[] imageviews) {
		imgviewGaller.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View view,
					int position, long arg3) {
				GalleryclickNumber(position, imgnumberliner, down, up,
						imageviews);
				if(FocusCDDataShow.gText!=null&&FocusCDDataShow.gtextnumber.size()>0)
				{
					FocusCDDataShow.gText.setText(FocusCDDataShow.gtextnumber.get(position%FocusCDDataShow.gtextnumber.size()));
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
//		imgviewGaller.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//					long arg3) {
//				
//				    
//				    Log.e("sss", "12123456791111111111111111111");
//			}
//		});
	}

	/**
	 * 根据画廊的滑动设置原点
	 * 
	 * @param postion
	 * @param imgnumberliner
	 */
	public void GalleryclickNumber(int postion, LinearLayout imgnumberliner,
			int down, int up, ImageView[] imageviews) {

		// ----滑动图片小圆点切换-----start---
		int n = postion % (imgnumberliner.getChildCount());// 取余数
		//Log.e("taggg", "" + imgnumberliner.getChildCount() + "bbbbmmm" + n);
		for (int i = 0; i < imgnumberliner.getChildCount(); i++) {

			imageviews[n].setImageResource(down);
			if (n != i) {
				imageviews[i].setImageResource(up);
			}
		}
		// -----end-----
	}
	
}
